package com.xcj.juhe.controller;

import com.xcj.juhe.dto.*;
import com.xcj.juhe.service.InitService;
import com.xcj.juhe.service.OrderService;
import com.xcj.juhe.service.RoleService;
import com.xcj.juhe.service.UserService;
import com.xcj.juhe.vo.RegisterUserVO;
import com.xcj.juhe.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xcj
 * @date 2021/4/14 19:08
 */
@RestController
public class OperationController {

    @Autowired
    UserService userService;

    @Autowired
    RoleService roleService;

    @Autowired
    InitService initService;

    @Autowired
    OrderService orderService;

    /**
     * 批量初始化设备
     * @param initDto
     * @return
     */
    @PostMapping(value = "/addInitDeviceBatch")
    public ResultVO addInitDeviceBatch(@RequestBody InitDto initDto) {
//, @RequestParam(value = "number",required=true) int number
        ResultVO resultVO = initService.addInitDevice(initDto);

        return resultVO;
    }

    /**
     * 使用线程池 批量初始化设备
     * @param initDto
     * @return
     */
    @PostMapping(value = "/addInitDeviceBatch2")
    public ResultVO addInitDeviceBatch2(@RequestBody InitDto initDto) {
//, @RequestParam(value = "number",required=true) int number
        ResultVO resultVO = initService.addInitDevice2(initDto);

        return resultVO;
    }

    /**
     * 批量注册账号
     * @param userDto
     * @return
     */
    @PostMapping(value = "/addUserBatch")
    public ResultVO addUserBatch(@RequestBody UserDto userDto) {

        ResultVO resultVO = new ResultVO();

        Map<String, Object> map = new HashMap<>();
        map.put("channelId", userDto.getChannelId());
        map.put("subChannelId", 0);
        map.put("realUserInfo", "");
        map.put("sign", "");

        //调用联运的注册接口
        ArrayList<RegisterUserVO> registerUserVOS = userService.addAccount(userDto.getNumber(),userDto.getDcProductId(),userDto.getDcChannelId(),userDto.getDcAppkey(),userDto.getTime());

        //调用聚合的注册接口
        if (registerUserVOS != null) {
            resultVO = userService.addUser(registerUserVOS, userDto.getAppId(), userDto.getAppKey(), map);
        } else {
            resultVO.setCode(100);
            resultVO.setMsg("联运都注册失败了");
        }

        return resultVO;
    }

    /**
     * 获取可以登录的小号信息
     * @param dcChannelId
     * @param dcProductId
     * @return
     */
    @GetMapping(value = "getCanLoginSubUserIds")
    public ResultVO getCanLoginSubUserIds(String dcChannelId,String dcProductId,String time){

        ArrayList<String> canLoginSubUserIds = userService.getCanLoginSubUserIds(dcChannelId,dcProductId,time);

        ResultVO resultVO = new ResultVO();
        resultVO.setCode(200);
        resultVO.setMsg("可进行登录操作的小号集合");
        resultVO.setData(canLoginSubUserIds);

        return resultVO;

    }

    /**
     * 批量账号登录
     * @param userLoginDto
     * @return
     */
    @PostMapping(value = "/userLoginBatch")
    public ResultVO userLoginBatch(@RequestBody UserLoginDto userLoginDto) {
        return userService.usersLogin(userLoginDto);
    }

    /**
     * 批量创建角色
     * @param roleDto
     * @return
     */
    @PostMapping(value = "/addRoleBatch")
    public ResultVO addRoleBatch(@RequestBody RoleDto roleDto) {
        ResultVO resultVO = roleService.addRole(roleDto);
        return resultVO;
    }

    /**
     * 批量下单
     * @param orderDtos
     * @return
     */
    @PostMapping(value = "/addOrderBatch")
    public ResultVO addOrderBatch(@RequestBody ArrayList<OrderDto> orderDtos){

        ResultVO resultVO = orderService.addOrder(orderDtos);
        return resultVO;
    }

}
